// pages/my/my.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    bgimg: null,
    videoList: [],
    page: 1,
    shanchuan: "上传背景图"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.cloud.database().collection("bgimg").get().then(res => {
      if(res.data.length>0){
        this.setData({
          bgimg: res.data[0].headpath,
          shanchuan: "更改背景图"
        })
      }
    }).catch(er => {
      this.setData({
        shanchuan: "上传背景图"
      })
    })
   
  },
  onShow() {
    if (this.data.userInfo) {
      console.log(1);
      this.getUserCollect();
    }

  },
  getUserCollect() {
    wx.showLoading({
      title: '正在加载..',
    })
    let videoCollect = wx.cloud.database().collection('video_collect');
    let skipNum = (this.data.page - 1) * 5;
    videoCollect.orderBy("coltime", "desc").limit(5).skip(skipNum).get()
      .then(result => {
        let videoList = this.data.videoList;
        videoList = videoList.concat(result.data); //链接合并数组
        this.setData({
          videoList: videoList
        });
        wx.hideLoading({
          success: (res) => {},
        })
      }).catch(er => {
      
        wx.showToast({
          icon: "error",
          title: '网络错误稍后重试'
        })
      }).finally(() => {
    
      })
      
      
  },
  foo() {
    wx.getUserProfile({
      desc: "授权"
    }).then(result => {
      this.setData({
        userInfo: result.userInfo
      })
      this.getUserCollect();
      app.globalData.userInfo = this.data.userInfo;
      wx.showToast({
        title: '授权成功',
      })
      wx.cloud.callFunction({
        name:"loginsave",
      }).then(re=>{
        let openid=re.result.openid;
        app.globalData.userInfo.openid=openid;
        result.openid=openid
        this.setData({
          userInfo:result
        })
      
      })
    }).catch(err => {
      wx.showToast({
        icon: "error",
        title: '授权失败',
      })
    })
  },
 
  foo1(){
    //在手机端通过文件 或 相机选择一张图片
    wx.chooseImage({
      count:1,//设置图片数量
    }).then(result=>{
      let path=result.tempFilePaths[0];//得到选择图片临时存储路径.
      this.setData({
        bgimg:path
      });
      //上传到云存储
      let headimgname=new Date().getTime()+""+Math.random(); //上传文件名称是一个随机名称
      let extname=path.substr(path.indexOf("."));//获取上传文件后缀.
      // console.log('后缀名称',extname)
      wx.cloud.uploadFile({
        cloudPath:"headimg/head"+headimgname+extname,//设置云存储路径
        filePath:path//设置本地上传的文件临时路径
      }).then(re=>{
        wx.showToast({
          title:"上传成功"
        });
        this.setHeadImage(re.fileID);//保存到数据库
        // console.log("云上传成功的结果==>",re)
      }).catch((err)=>{
        // console.log(err);.
        wx.showToast({
          title:"上传失败"
        })
      })
      // console.log(result)
    })
  },
  setHeadImage(path){
    let headimg=wx.cloud.database().collection("bgimg");
    console.log(path);
        headimg.get().then(result=>{
          console.log(result);
          if(result.data.length>0){
            console.log(1);
            //更新头像 doc("_id") 根据id更新数据 .where()
            headimg.doc(result.data[0]._id).update({
              data:{
                headpath:path
              }
            }).then(re=>{
              wx.showToast({
                title: '头像保存成功',
              })
            }).catch(er=>{
              wx.showToast({
                icon:"error",
                title: '头像保存失败',
              })
            })
          }else{
            console.log(2);
            //添加头像信息
            headimg.add({
             data:{
              headpath:path,
             }
            }).then(re=>{
              wx.showToast({
                title: '头像保存成功',
              })
            }).catch(er=>{
              wx.showToast({
                icon:"error",
                title: '头像保存失败',
              })
            })
          }
        }) 
  },



  onReachBottom() {
  if (this.data.userInfo) {
    
    let page = this.data.page;
    ++page;
    this.setData({
      page: page
    });
    this.getUserCollect();
  }
  },
  clearCollect(){
    wx.cloud.callFunction({
      name:"mycloudfuntion",
      data:{
        action:"clear"
      }
    }).then(re=>{
      if(re.result.result.stats.removed){
        wx.showToast({
          title: '删除'+re.result.result.stats.removed+"条数据",
        });
        this.setData({
          videoList:[]
        })
      }
    })
  }
})